// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.appengine.syncserver.shared;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * The server response which contains the client id.
 */
public class ClientIdResponse {

  private static final String CLIENT_ID = "clientId";
  
  private static final Logger log = Logger.getLogger(ClientIdRequest.class.getName());

  /**
   * Converts serialized json string to {@link ClientIdResponse} object.
   * 
   * @param str
   * @return {@link ClientIdResponse} as read from the json string.
   */
  public static ClientIdResponse valueOf(String str)
      throws JsonParseException, JsonMappingException, IOException {
    return (ClientIdResponse) JsonUtils.stringToObject(str, ClientIdResponse.class);
  }

  private final long clientId;

  @JsonCreator
  public ClientIdResponse(@JsonProperty(CLIENT_ID) long clientId) {
    this.clientId = clientId;
  }

  /**
   * @return The server generated client in the response object
   */
  @JsonProperty(CLIENT_ID)
  public long getClientId() {
    return clientId;
  }

  // TODO(chintanraoh): dont override toString.
  @Override
  public String toString() {
    try {
      return JsonUtils.objectToString(this);
    } catch (JsonGenerationException e) {
      log.severe(e.getMessage());
    } catch (JsonMappingException e) {
      log.severe(e.getMessage());
    } catch (IOException e) {
      log.severe(e.getMessage());
    }
    return null;
  }
}
